<?php
/**
 * 获取推送不同接口.
 *
 * @author mayy
 * @date 2017-7-25
 */
namespace app\common\com\push;

define( 'Push_ROOT', dirname(__FILE__) );

class PushFactory {

	/**
	 * 获取一个推送发送对象
     *
	 * @param string $type		推送驱动类型
	 * @param array $config		推送配置参数
	 * @return object
	 */
	public static function getPush($type = '', array $config = []) {

		static $return;

		$push_config = config('extra.push_config');

		$cfg = !empty($push_config)          ? $push_config['config'] : $config;
		$type = !empty($push_config['type']) ? $push_config['type']   : $type;

		if(!empty($return) && ($return instanceof PushBase)) {
			return $return;
		}

		require_once (Push_ROOT . '/interface/' . $type . '.php');

		$push = new $type($cfg);
		if(!$push instanceof PushBase) {
			exception('Push_interface_invalid');
		}
		$return = $push;
		return $return;
	}

	/**
	 * 生成缓存key
     *
	 * @param string $type		推送驱动类型
	 * @param array $config		推送配置参数
	 * @return string
	 */
	private static function get_key($type, array $config)
	{
		$key = $type;
		foreach($config as $k => $v) {
			$key .= $k . $v;
		}
		return md5($key);
	}
	
}